package cn.com.egova.permission.szzt;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;

import java.util.List;

import cn.com.egova.permission.R;
import cn.com.egova.permission.szzt.lib.AndPermission;
import cn.com.egova.permission.szzt.lib.Rationale;
import cn.com.egova.permission.szzt.lib.RationaleListener;

/**
 * Created by hsy on 2017/3/14.
 * 权限工具
 */
public class PermissionUtil {
    //读写sdcard权限
    public static final String READ_SDCARD = Manifest.permission.READ_EXTERNAL_STORAGE;
    public static final String WRITE_SDCARD = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    //定位权限
    public static final String ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    public static final String READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    //拍照权限
    public static final String TAKE_CAMERA = Manifest.permission.CAMERA;
    //录音权限
    public static final String TAKE_AUDIO = Manifest.permission.RECORD_AUDIO;
    //拨号权限
    public static final String CALL_PHONE = Manifest.permission.CALL_PHONE;
    //发短信
    public static final String SEND_SMS = Manifest.permission.SEND_SMS;
    //声音控制
    public static final String MODIFY_AUDIO = Manifest.permission.MODIFY_AUDIO_SETTINGS;

    /**
     * 请求码
     */
    public static final int REQUEST_CODE_PERMISSION_OTHER = 11;
    public static final int REQUEST_CODE_PERMISSION_SETTING = 12;

    //Match ACCESS_FINE_LOCATION
    public static final int REQUEST_CODE_PERMISSION_GPS = 95;

    public static final String[] BASIC_PERMISSIONS = new String[]{ READ_SDCARD, WRITE_SDCARD, ACCESS_FINE_LOCATION,
            ACCESS_COARSE_LOCATION, READ_PHONE_STATE,MODIFY_AUDIO };

    /**
     * @param requestCode 兼容之前代码，requestCode总为{@link PermissionUtil#REQUEST_CODE_PERMISSION_SETTING}
     */
    public static void afterFailPermission(Activity activity, int requestCode,
        List<String> deniedPermissions) {
        afterFailPermission(activity, deniedPermissions, false, null, null);
    }

    /**
     * @param requestCode 兼容之前代码，requestCode总为{@link PermissionUtil#REQUEST_CODE_PERMISSION_SETTING}
     */
    public static void afterFailPermission(Activity activity, int requestCode,
                                           List<String> deniedPermissions, final Runnable runnable) {
        boolean isShowDialog = afterFailPermission(activity, deniedPermissions, false, null, runnable);
        if (!isShowDialog) {
            if (runnable != null) {
                runnable.run();
            }
        }
    }

    public static boolean afterFailPermission(Activity activity, List<String> deniedPermissions, boolean alwaysShow,
                                              final Runnable confirmAction, final Runnable cancelAction) {
        return afterFailPermission(activity, PermissionUtil.REQUEST_CODE_PERMISSION_SETTING, deniedPermissions,
                alwaysShow, confirmAction, cancelAction);
    }

    public static boolean afterFailPermission(Activity activity, int requestCode, List<String> deniedPermissions, boolean alwaysShow,
                                           final Runnable confirmAction, final Runnable cancelAction) {
        if (alwaysShow || AndPermission.hasAlwaysDeniedPermission(activity, deniedPermissions)) {
            AndPermission.defaultSettingDialog(activity, requestCode)
                    .setTitle(R.string.permission_title_permission_failed)
                    .setMessage(R.string.permission_apply_fail)
                    .setPositiveButton(R.string.permission_setting)
                    .setPositiveAction(confirmAction)
                    .setNegativeAction(cancelAction)
                    .setNegativeButton(R.string.permission_cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if(null != cancelAction) {
                                cancelAction.run();
                            }
                        }
                    })
                    .show();
            return true;
        } else {
            return false;
        }
    }

    public static boolean checkPermissions(final Activity activity, String... permissions) {
        return checkPermissions(activity, REQUEST_CODE_PERMISSION_OTHER, permissions);
    }

    public static boolean checkPermissions(final Fragment fragment, String... permissions) {
        return checkPermissions(fragment, REQUEST_CODE_PERMISSION_OTHER, permissions);
    }

    public static boolean checkPermissions(final Activity activity, int requestCode,
        String... permissions) {
        if (!AndPermission.hasPermission(activity, permissions)) {
            requestPermission(activity, requestCode, permissions);
            return false;
        } else {
            return true;
        }
    }

    public static boolean checkPermissions(final Fragment fragment, int requestCode,
                                           String... permissions) {
        if (!AndPermission.hasPermission(fragment.getActivity(), permissions)) {
            requestPermission(fragment, requestCode, permissions);
            return false;
        } else {
            return true;
        }
    }

    public static void requestPermission(final Activity activity, String... permissions) {
        requestPermission(activity, REQUEST_CODE_PERMISSION_OTHER, permissions);
    }

    public static void requestPermission(final Fragment fragment, String... permissions) {
        requestPermission(fragment, REQUEST_CODE_PERMISSION_OTHER, permissions);
    }

    public static void requestPermission(final Activity activity, int requestCode,
        String... permissions) {
        AndPermission.with(activity)
            .requestCode(requestCode)
            .permission(permissions)
            .rationale(new RationaleListener() {
                @Override
                public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                    AndPermission.rationaleDialog(activity, rationale).show();
                }
            })
            .send();
    }

    public static void requestPermission(final Activity activity, int requestCode,RationaleListener listener,
                                         String... permissions) {
        AndPermission.with(activity)
                .requestCode(requestCode)
                .permission(permissions)
                .rationale(listener)
                .send();
    }

    public static void requestPermission(final Fragment fragment, int requestCode,
                                         String... permissions) {
        AndPermission.with(fragment)
            .requestCode(requestCode)
            .permission(permissions)
            .rationale(new RationaleListener() {
                @Override
                public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                    AndPermission.rationaleDialog(fragment.getActivity(), rationale).show();
                }
            })
            .send();
    }

    /**
     * 检测存储权限
     */
    public static void checkPermissionStorage(final Activity activity) {
        checkPermissions(activity, PermissionUtil.READ_SDCARD, PermissionUtil.WRITE_SDCARD);
    }

    /**
     * 检测存储和定位权限
     */
    public static void checkPermissionStoreLocation(final Fragment fragment) {
        checkPermissions(fragment, READ_SDCARD, WRITE_SDCARD, ACCESS_FINE_LOCATION,
                ACCESS_COARSE_LOCATION, READ_PHONE_STATE);
    }
    public static boolean checkPermissionStoreLocation(final Activity activity) {
        return checkPermissions(activity, READ_SDCARD, WRITE_SDCARD, ACCESS_FINE_LOCATION,
            ACCESS_COARSE_LOCATION, READ_PHONE_STATE,MODIFY_AUDIO);
    }

    /**
     * 检测定位权限
     * @param activity
     * @return
     */
    public static boolean checkPermissionLocation(final Activity activity) {
        return checkPermissions(activity, ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION);
    }

    public static boolean checkPermissionBasic(final Activity activity) {
        return checkPermissions(activity, BASIC_PERMISSIONS);
    }
}
